package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.service.AttrService;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.service.SkuAttrValueService;


@Service("skuAttrValueService")
public class SkuAttrValueServiceImpl extends ServiceImpl<SkuAttrValueMapper, SkuAttrValueEntity> implements SkuAttrValueService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SkuAttrValueEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SkuAttrValueEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<SkuAttrValueEntity> getSearchAttrBySkuIdAndCId(Long skuId, Long cId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("search_type",1).eq("type",0).eq("category_id",cId);
        List<AttrEntity> attrEntities = attrService.list(wrapper);

        if (Collections.isEmpty(attrEntities)){
            return null;
        }

        List<Long> longs = attrEntities.stream().map(attrEntity -> {
            return attrEntity.getId();
        }).collect(Collectors.toList());

        QueryWrapper<SkuAttrValueEntity> skuAttrWrapper = new QueryWrapper<>();
        skuAttrWrapper.eq("sku_id",skuId).in("attr_id",longs);
        List<SkuAttrValueEntity> list = this.list(skuAttrWrapper);
        if (Collections.isEmpty(list)){
            return null;
        }

        return list;
    }

}